<?php

require_once ('lib/class.http.php');
require_once ('lib/dump.php');
require_once ('lib/class.urls.php');
include_once ('lib/parvis.constants.php');

// Retrouver la langue de l'URL
$langage = urls::getLangNum();
ob_start();
$api = new Http();
$api->debug = $debug;

//$url = 'http://parvis.ch/cgi-bin/cgiip.exe/WService=wsbroker1/tbr_commande.w';
$url = sprintf('%stbr_commande.w', WEBSPEED);

$api->useCurl(false);


if (empty($_POST)) {
	// 1er appel: GET
    $api->setMethod('GET');

    $api->addParam('lang', $langage);
    $api->addParam('id', $_GET['id']);
    $api->addParam('retour', ($_GET['retour']));

} else {
   //session_start();
   //foreach($_POST as $var => $value) {
   //	$_SESSION[$var] = $value;
   //}


	// Appels suivants (via un des boutons de l'interface)
    $api->setMethod('POST');

    // Les paramètres de POST sont redécodés en ISO
    foreach ($_POST as $key => $post) {
	    $param = utf8_decode($_POST[$key]);

	    $api->addParam($key, $param);
	}
}

$api->useCookie(true);
$api->saveCookie(true);

$api->addCookie('SessionId', $_COOKIE['SessionId']);
$api->addCookie('InterNou', $_COOKIE['InterNou']);

$api->execute($url);

$cookies = $api->_cookies;

if ($debug) {
	dump($api->params, 'POST Params: ');
	dump($cookies, 'Cookies retournés par le serveur');

	dump($cookies[0]['value'], 'value');
}


if (!empty($cookies[0])) {
	// Stockage "local" du cookie retourné par le serveur
	if ($cookies[0]['name'] == 'InterNou') {
		setcookie('InterNou', $cookies[0]['value'], COOKIES_TIME, COOKIES_PATH);
	}
	if ($cookies[0]['name'] == 'SessionId') {

		setcookie('SessionId', $cookies[0]['value'], COOKIES_TIME, COOKIES_PATH);
	}
}




echo ($api->error) ? $api->error : utf8_encode($api->result);
$content = ob_get_contents();

ob_end_clean();
$action= strip_tags($content);
//$action = preg_replace('/\s+/', '', $action);
$action = trim($action);

$parts = explode(" ",$action);
$tmp = substr($action, 0, 100);
if(strpos($tmp, "|") !== false){
	$parts = explode("|",$action);
}
$parts[0] = trim($parts[0]);
//print '|'.$parts[0].'|';
//print_r($parts);
//echo"%$content%";
include("switch.php");


?>
